package com.live.shop.tool;

import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkHttpUtils {

    //发送jSON格式的请求
    public static void sendOkHttpJsonRequest(String url,String jsonStr ,okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonStr);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/json")
                .post(body)
                .build();
        client.newCall(request)
                .enqueue(callback);
    }

    //发送jSON格式的请求
    public static void sendOkHttpJsonTokenRequest(String url,String jsonStr ,okhttp3.Callback callback,String token) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonStr);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/json")
                .addHeader("token", token==null?"":token)
                .post(body)
                .build();
        client.newCall(request)
                .enqueue(callback);
    }

    //发送字符串的请求
    public static void sendOkHttpStringRequest(String url,String str ,okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=utf-8"), str);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "text/html;charset=utf-8")
                .post(body)
                .build();
        client.newCall(request)
                .enqueue(callback);
    }
    //发送字符串
    public static void sendOkHttpErrorRequest(String url,String str,String token){
        Runnable networkTask = new Runnable() {
            @Override
            public void run() {
                // 在这里执行网络请求
                try {
                    // 这里替换成您的网络请求实现，例如使用 OkHttp 库
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json = new JSONObject();
                    json.put("error",str);
                    RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), json.toString());
                    Request request = new Request.Builder()
                            .url(url)
                            .addHeader("Content-Type", "application/json")
                            .addHeader("token", token==null?"":token)
                            .post(body)
                            .build();
                    client.newCall(request).execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        // 在子线程中执行网络请求任务
        Thread networkThread = new Thread(networkTask);
        networkThread.start();
    }
}
